<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">任务编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="Id" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">任务类型</label>
                            <div class="layui-input-inline">
                                <select type="text" name="type" id="selectType" class="layui-input">
                                    <option value="">请选择类型</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">任务状态</label>
                            <div class="layui-input-inline">
                                <select type="text" name="status" id="selectStatus" class="layui-input">
                                    <option value="">请选择状态</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">分配机器人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="robotId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">创建时间</label>
                            <div class="layui-input-inline">
                                <input type="datetime" name="createTime" id="createTime" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="refresh"> 刷新 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <script type="text/html" id="stateBar0">
            <div>
                {{# if(d.status=== 0){ }}
                未执行
                {{# }else if(d.status=== 1){ }}
                前往接料
                {{# }else if(d.status=== 2){ }}
                接料中
                {{# }else if(d.status=== 3){ }}
                接料完成
                {{# }else if(d.status=== 4){ }}
                前往送料
                {{# }else if(d.status=== 5){ }}
                送料中
                {{# }else if(d.status=== 6){ }}
                执行中
                {{# }else if(d.status=== 7){ }}
                已完成
                {{# }else if(d.status=== 8){ }}
                被打断
                {{# }else if(d.status=== 9){ }}
                被取消
                {{# }else if(d.status=== 10){ }}
                充电中
                {{# }else{ }}
                未知
                {{# } }}
            </div>
        </script>
        <script type="text/html" id="tasktype">
            <div>
                {{# if(d.type=== 1){ }}
                接料任务
                {{# }else if(d.type=== 2){ }}
                送料任务
                {{# }else if(d.type=== 3){ }}
                接送料任务
                {{# }else if(d.type=== 4){ }}
                充电任务
                {{# }else{ }}
                未知
                {{# } }}
            </div>
        </script>
        <script type="text/html" id="beginTimeStatus">
            <div>
                {{#  if(d.beginTime !=null){ }}
                <div>{{layui.util.toDateString(d.beginTime, 'HH:mm:ss')}}</div>
                {{#  } else {}}
                <div>{{}}</div>
                {{#  } }}

            </div>

        </script>

        <script type="text/html" id="updateTimeStatus">
            <div>
                {{#  if(d.updateTime !=null){ }}
                <div>{{layui.util.toDateString(d.updateTime, 'HH:mm:ss')}}</div>
                {{#  } else {}}
                <div>{{}}</div>
                {{#  } }}

            </div>

        </script>
    </div>
</div>

<script>
    layui.use(['form', 'table', 'miniPage', 'element', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
            laydate = layui.laydate;

        laydate.render({     //创建时间选择框
            elem: '#createTime' //指定元素
        });

        table.render({
            elem: '#currentTableId',
            url: 'api/Job/GetTaskByPage',
            parseData: function (res) { //res 即为原始返回的数据
                return {

                    "code": res.code === 200 ? 0 : res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.result, //解析数据长度
                    "data": res.data //解析数据列表
                }
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                { field: 'id', width: 110, title: '任务编号', sort: true, align: "center"},
                { field: 'createTime', title: '创建时间', width: 160, sort: true, align: "center", templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>" },
                { field: 'beginTime', width: 120, title: '开始时间', sort: true, align: "center", templet: "#beginTimeStatus" },
                { field: 'updateTime', width: 120, title: '更新时间', sort: true, align: "center", templet: "#updateTimeStatus" },
                { field: 'priority', width: 80, title: '优先级', align: "center" },
                { field: 'targets', width: 150, title: '目标点', align: "center" },
                { field: 'status', width: 100, title: '任务状态', align: "center", templet:"#stateBar0" },
                { field: 'type', width: 120, title: '任务类型', align: "center", templet:"#tasktype" },
                { field: 'robotId', width: 120, title: '分配机器人', align: "center" },
                { field: 'remark', minwidth: 100, title: '备注', align: "center" },
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            id:"reloadTable",
            even:"true",
            skin: 'row'
        });

        //表格重新加载
        var tablereload = function (search) {
            table.reload('reloadTable',
                {
                    where: {
                        query: search
                    }, page: {
                        curr: 1
                    }
                });
        }

        //类型动态添加下拉框     同时可以设置默认值
        $.ajax({
            type: 'get',
            url: 'api/Job/GetJobTypes/',
            dataType: 'json',
            success: function (res) {
                $.each(res.data, function (key, val) {
                    var option1 = $("<option>").val(val).text(key);
                    //通过LayUI.jQuery添加列表项
                    $("#selectType").append(option1);
                });
                form.render('select');
                $("#selectType").get(0).selectedIndex = 0;
            }
        });

        //状态动态添加下拉框     同时可以设置默认值
        $.ajax({
            type: 'get',
            url: 'api/Job/GetJobStatus/',
            dataType: 'json',
            success: function (res) {
                $.each(res.data, function (key, val) {
                    var option1 = $("<option>").val(val).text(key);
                    //通过LayUI.jQuery添加列表项
                    $("#selectStatus").append(option1);
                });
                form.render('select');
                $("#selectStatus").get(0).selectedIndex = 0;
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            var query = {};
            for (var key in data.field) {
                if (data.field[key].length > 0) {
                    query[key] = data.field[key];
                }
            }
            table.reload('reloadTable', {
                where: {
                    query: JSON.stringify(query)
                }
                , page: {
                    curr: 1
                }
            });

            return false;
        });

      
        //删除添加
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'refresh') {   // 刷新操作
                tablereload("");
            } else if (obj.event === 'delete') {  // 监听删除操作
                layer.confirm('您确定要删除吗?', function (index) {
                    layer.close(index);
                    // 监听删除操作
                    var checkStatus = table.checkStatus('reloadTable')
                        , data = checkStatus.data;
                    var ids = [];
                    data.forEach(x => { ids.push(x.id); });
                    $.ajax({
                        url: 'api/Job/DeleteByIds',
                        method: 'delete',
                        contentType: 'application/json;charset=UTF-8',
                        cache: false,
                        data: JSON.stringify(ids),
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg('删除成功!', { icon: 6 },
                                    function () {
                                        //修改成功后刷新父界面
                                        tablereload("");
                                    });
                            } else
                                alert(res.msg);
                        },
                        error: function (data) {

                            layer.msg('删除失败!');

                        }
                    });
                })
            }
            
        });

       
        //编辑删除
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var content = miniPage.getHrefContent('page/table/add.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '编辑AGV',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('您确定要删除吗?', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }
        });

    });
</script>